//
//  BKResultOfLiuXueViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/6.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKResultOfLiuXueViewController.h"
#import "BKBKLiuXueTableViewCell.h"
#import "BKLiuXueTableViewCell.h"
#import "BKBlackTextField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKLiuXueViewModel.h"
#import <MBProgressHUD.h>
static NSString *liuxue = @"xxxxx";

@interface BKResultOfLiuXueViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet BKBlackTextField *pici;
@property (weak, nonatomic) IBOutlet BKBlackTextField *dq;
@property (weak, nonatomic) IBOutlet BKBlackTextField *lx;
@property (weak, nonatomic) IBOutlet BKBlackTextField *gj;


@end

@implementation BKResultOfLiuXueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKLiuXueTableViewCell class]) bundle:nil] forCellReuseIdentifier:liuxue];
    _tableView.separatorColor= [UIColor clearColor];
    [_tableView reloadData];
    
    _gj.dataArray = _gjArray;
    _gj.flag = 8011;
    
    @weakify(self);
    [[_pici rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
    [[_dq rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
    [[_lx rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
    [[_gj rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)p_reloadData{


    NSString *bz = @"";
    if ([_pici.text hasPrefix:@"本"]) {
        bz = @"1";
        
    }
    
    if ([_pici.text hasPrefix:@"专"]) {
        bz = @"2";
   
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载留学信息";
    [hud show:YES];

    
    __weak BKResultOfLiuXueViewController *weakSelf = self;
    [BKLiuXueViewModel liuxueWithbz:bz dq:_dq.text fw:_lx.text gj:_gj.text success:^(NSArray *dataArray) {
    
        [hud hide:YES];
        [hud removeFromSuperview];
        weakSelf.dataArray = dataArray;
        [weakSelf.tableView setScrollsToTop:YES];
        [weakSelf.tableView reloadData];
    } failure:^(NSString *message) {
        [hud hide:YES];
        [hud removeFromSuperview];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];

    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BKLiuXueTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:liuxue];
    if (indexPath.row%2==0) {
        cell.backgroundColor = [UIColor clearColor];
    }else{
        cell.backgroundColor = [UIColor colorWithRed:0.914 green:0.965 blue:0.980 alpha:1.000];
    }
    
    
    for (UIView *view in [cell.major subviews]) {
        [view removeFromSuperview];
    }
    
    if (indexPath.row ==0 ) {
        cell.gaoxiao.text = @"高校";
        cell.name.text = @"合作学校";
        cell.xiangmu.text = @"项目";
       
    }else{
        
        cell.name.text = _dataArray[indexPath.row-1][@"xuexiao"];
        cell.xiangmu.text = _dataArray[indexPath.row-1][@"xiangmu"];
        cell.gaoxiao.text = _dataArray[indexPath.row-1][@"name"];
        
       
        
        NSArray *array = _dataArray[indexPath.row-1][@"major"];
        
        for (int i=0; i<[array count]; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*44, tableView.frame.size.width/3, 44)];
            label.text = array[i];
            [cell.major addSubview:label];
        }
        
    }
    return cell;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_dataArray isEqual:[NSNull null]]) {
        return 1;
    }
    return [_dataArray count]+1;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 44;
    }
    
    NSString *xiangmu = _dataArray[indexPath.row-1][@"xiangmu"];
    NSString *name = _dataArray[indexPath.row-1][@"name"];
    NSString *xuexiao = _dataArray[indexPath.row-1][@"xuexiao"];
    
    
    UIFont *textFont = [UIFont systemFontOfSize:17];
    
    CGSize size = CGSizeMake(tableView.frame.size.width/3-30, 1000);
    
    //获取当前那本属性
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];
    //实际尺寸
    CGSize actualSize1 = [xiangmu boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    CGSize actualSize2= [name boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    CGSize actualSize3= [xuexiao boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    
    //NSArray *array = _dataArray[indexPath.row-1][@"major"];
    
    if (actualSize3.height>actualSize1.height) {
        if (actualSize3.height>actualSize2.height) {
        
            return  actualSize3.height + 2;
        }else{
        
            return actualSize2.height + 2;
           
        }
       
    }else{
    
        if (actualSize2.height>actualSize1.height) {
            return actualSize2.height + 2;
        }else{
        
            return actualSize1.height + 2;
        }
    }
   // return 44;
    
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}


@end
